package com.tsy.server.component;

import com.dji.sdk.common.HttpResultResponse;
import com.tsy.server.common.Result;
import com.tsy.server.common.page.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author sean
 * @version 0.2
 * @date 2021/12/1
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理参数验证异常
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
    public Result<Void> handleValidException(Exception e) {
        BindingResult bindingResult = null;
        if (e instanceof MethodArgumentNotValidException) {
            bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
        } else if (e instanceof BindException) {
            bindingResult = ((BindException) e).getBindingResult();
        }

        if (bindingResult != null && bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                return Result.validateFailed(fieldError.getDefaultMessage());
            }
        }
        return Result.validateFailed();
    }

    /**
     * 处理认证异常
     */
    @ExceptionHandler(value = BadCredentialsException.class)
    public Result<Void> handleAuthenticationException(BadCredentialsException e) {
        log.error("认证异常：{}", e.getMessage(), e);
        return Result.failed(ResultCode.USERNAME_NOT_EXISTS.getMessage());
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(value = RuntimeException.class)
    public Result<Void> handleRuntimeException(RuntimeException e) {
        log.error("业务异常：{}", e.getMessage(), e);
        return Result.failed(e.getMessage());
    }

    /**
     * 处理未知异常
     */
    @ExceptionHandler(value = Exception.class)
    public Result<Void> handleException(Exception e) {
        log.error("系统异常：{}", e.getMessage(), e);
        return Result.failed("系统异常，请联系管理员");
    }

//    /**
//     * Please do not return directly like this, there is a risk.
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(Exception.class)
//    public HttpResultResponse exceptionHandler(Exception e) {
//        e.printStackTrace();
//        return HttpResultResponse.error(e.getLocalizedMessage());
//    }
//
//    @ExceptionHandler(NullPointerException.class)
//    public HttpResultResponse nullPointerExceptionHandler(NullPointerException e) {
//        e.printStackTrace();
//        return HttpResultResponse.error("A null object appeared.");
//    }

//    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
//    public HttpResultResponse methodArgumentNotValidExceptionHandler(BindException e) {
//        e.printStackTrace();
//        return HttpResultResponse.error(e.getFieldError().getField() + e.getFieldError().getDefaultMessage());
//    }

}
